import { subscribeWithSelector } from 'zustand/middleware'
import create from 'zustand'
import { FetchAllMeetingDetail } from '@/api/create/interface'
import { getOptionValue } from '@/pages/sysConfig/virtualRoomConfig/translator'
import { StateStore } from './interface'
import { FetchTemplateParams } from '@/api/template/interface'
import { getSelfMediaAbilityVal } from './store.media'

type IStore = StateStore & {
	update: (d: Partial<StateStore>) => void
	reset: () => void
	init: (model: FetchAllMeetingDetail | FetchTemplateParams) => void
}

const initialState: StateStore = {
	meetingType: 1,
	voiceInspireEnable: false,
	voiceInspireTime: 3,
	encryptedAuth: false,
	encryptedKey: '',
	encryptedSM: false,
	encryptionType: 0,
	quantumEncryption: false,
	enableRtc: false,
	initMute: false,
	allowAnonymousMt: false,
	muteException: true,
	meetingProtocol: 0,
	closedMeeting: false,
	meetingSafe: false,
	password: '',
	doubleFlowLimit: 1,
	sendDoubleFlowAtOnce: false,
	videoQuality: 0,
	callMode: 2,
	callInterval: '20',
	callTimes: '',
	callTimesUnlimited: true,
	cascadeReturn: false,
	cascadeReturnPara: '',
	cascadeUpload: true,
	oneReforming: false,
	fecMode: false,
	watermark: false,
	sourceUsed: 1,
	simpleCascade: 1,
	multiStream: false,
	frame: 30,
	rate: 2048,
	resolution: 12,
	video: 4,
	keep_calling_members: [],
	audio_formats: [],
	_H265: false,
	_bitrateType: 3,
	_meetingTypeList: [],

	_selfMediaList: [],
	_selfMediaValue: 0,
	_preRate: 2048,
}

export const useMeetingParamsStore = create<IStore>()(
	subscribeWithSelector((set, get) => ({
		...initialState,
		update: (d) => set({ ...d }),
		reset: () => set(initialState), //初始化会议类型
		init: (model) => {
			let bitrateType =
				model.conf_type === 2
					? model.video_formats[0].resolution === 12
						? 4
						: 6
					: getOptionValue({
							frame: model.video_formats[0].frame,
							rate: model.video_formats[0].bitrate,
							resolution: model.video_formats[0].resolution,
							video: model.video_formats[0].format,
					  })
			set({
				meetingType: model.conf_type,
				voiceInspireEnable: !!model.voice_activity_detection,
				voiceInspireTime: model.vacinterval,
				encryptedAuth: !!model.encrypted_auth,
				encryptedKey: model.encrypted_key,
				encryptedSM: model.encrypted_type === 4 ? true : false,
				encryptionType: model.encrypted_type,
				quantumEncryption: !!model.quantum_encryption,
				enableRtc: !!model.enable_rtc,
				initMute: !!model.mute,
				allowAnonymousMt: !!model.anonymous_mt,
				muteException: !!model.mute_filter,
				meetingProtocol: model.conf_protocol,
				closedMeeting: !!model.closed_conf,
				meetingSafe: !!model.safe_conf,
				password: model.password,
				doubleFlowLimit: model.dual_mode ?? 1,
				sendDoubleFlowAtOnce: !!model.doubleflow,
				videoQuality: model.video_quality,
				callMode: model.call_mode,
				callInterval: String(model.call_interval),
				callTimes: String(model.call_times),
				callTimesUnlimited: model.call_times === 0,
				cascadeReturn: !!model.cascade_return,
				cascadeReturnPara: String(model.cascade_return_para),
				cascadeUpload: !!model.cascade_upload,
				oneReforming: !!model.one_reforming,
				fecMode: !!model.fec_mode,
				watermark: !!model.watermark,
				sourceUsed: model.preoccupy_resource,
				simpleCascade: model.smart_cascade_mode ? 2 : model.cascade_mode,
				multiStream: !!model.multi_stream,
				frame: model.video_formats[0].frame,
				rate: model.video_formats[0].bitrate,
				resolution: model.video_formats[0].resolution,
				video: model.video_formats[0].format,
				keep_calling_members: model.keep_calling_members ?? [],
				audio_formats: model.audio_formats,
				_H265: model.video_formats[0].format === 6,
				_bitrateType: bitrateType,
				_preRate: model.video_formats[0].bitrate,
				_selfMediaValue:
					bitrateType === 0
						? getSelfMediaAbilityVal(model.video_formats[0].resolution, model.video_formats[0].frame)
						: 0,
			})
		},
	}))
)
